var Map = (function () {
    // 初始化

    function Map(options) {
        // 属性
        this.app = options.app;

        this.map2d = new Map2D();
        this.map3d = new Map3D();
        this.init();
    }
    //
    //初始化
    Map.prototype.init = function (mapdiv) {


        //初始化一个图层顺序，要不，后边加载图层的时候可能会被压盖
        //this.loadbase2d();
        //this.loadbase3d();
        this.initlayers();
        this.loadchinaline({
            "layerid": "basechinaline"
        });
        //
        this.map3d.initol3d(this);
    };
    //修改背景
    Map.prototype.setbasemap = function (options) {
        this.view = this.app.viewmode;
        if (this.view === "2d") {
            this.map2d.setbasemap({
                "layerid": options.value
            });
        } else if (this.view === "3d") {
            this.map3d.setbasemap({
                "layerid": options.value
            });
        }
        else if (this.view === "23d") {
            this.map2d.setbasemap({
                "layerid": options.value
            });
            this.map3d.setbasemap({
                "layerid": options.value
            });
        }
    };

    Map.prototype.loadbase3d = function () {
        var that = this;
        var layers = _.sortBy(layerconfig, 'index');
        $.each(layers, function (index, val) { });
    };
    Map.prototype.loadbase2d = function () {
        var layers = _.sortBy(layerconfig, 'index');
        $.each(layers, function (index, val) {
            var tmpLayer = new ol.layer.Vector({
                id: val.id,
                opacity: 0.95,
                maxzoom: 1224,
                minzoom: 0.0001
            });
            that.ol2d.olmap.addLayer(tmpLayer);
        });
    };

    Map.prototype.initlayers = function () {
        var that = this;

        this.alllayers = [
            new ol.layer.Image({
                id: "basechinaline", //
                title: "全球AQI数值",
                type: "aqi",
                region: "world",
                product: "gdt",
                group: "aqipoint",
                blur: 20,
                radius: 10,
                visible: true
            }),
            new ol.layer.Heatmap({
                id: "aqiwrltlayer", //
                title: "全球AQI热力图",
                type: "aqi",
                region: "world",
                product: "rlt",
                group: "aqiarea",
                blur: 20,
                radius: 10,
                visible: false
            }),
			/*new ol.layer.AnimatedCluster({
				id: "aqiwjhtlayer", //
				title: "全球AQI聚合图",
				type: "aqi",
				region: "world",
				product: "jht",
				group: "aqipoint",
				animationDuration: 1000,
				source: new ol.source.Cluster({
					distance: 40,
					source: new ol.source.Vector({
						features: []
					})
				}),
				visible:false
			}) ,*/
            new ol.layer.Image({
                id: "aqiwszlayer", //
                title: "全球AQI数值",
                type: "aqi",
                region: "world",
                product: "gdt",
                group: "aqipoint",
                blur: 20,
                radius: 10,
                visible: false
            })

        ];
        function changepop(e) {
            $(".text-lvl-1").css("color", "#FFF000");
        }
        // 设置点击事件模板
        var template = "<p>正在加载... ...</p>"; // {{station}}:<code>{{station}}{{station}}</code>
        //console.info(that.id);


        $.each(this.alllayers, function (i, val) {

            that.map2d.olmap.addLayer(val);

            that.map2d.clicks({
                "title": "name",
                "layerid": val.get('id'),
                "template": template,
                "script": changepop,
                "url": "views/aqidatapop.html", //"?layerid=" + that.id + "&id={{idx}}&tm=" + new Date().getTime()
            });
        });

    };

    Map.prototype.loadchinaline = function (options) {
        var that = this;
        var geostyle = function (feature, resolution) {

            var fillcolor = "rgba(255, 0, 0, 0)";

            //console.info(gtype);
            //console.info(fillcolor);
            var style = [new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: 'yellow',
                    width: 2
                }),
                fill: new ol.style.Fill({
                    color: fillcolor
                }),

            })];
            //console.info(style);
            return style;
        };
        var vectorSource = new ol.source.Vector({
            url: '/datas/gzline.json',
            format: new ol.format.GeoJSON(),
        });
        var imageVector = new ol.source.ImageVector({
            source: vectorSource,
            style: geostyle
        });

        var tmpLayer = that.map2d.getlayerbyid({
            "layerid": "basechinaline",
        });

        tmpLayer.setSource(null);
        tmpLayer.setSource(imageVector);

        
    }
    // 返回
    return Map;
})();
